package com.wood.life.service.Impl;

import com.night.common.entity.ResultVo;
import com.night.common.entity.life.Goods;
import com.night.common.entity.life.Order;
import com.night.common.entity.life.OrderDetails;
import com.wood.life.mapper.OrderDetailsMapper;
import com.wood.life.mapper.OrderMapper;
import com.wood.life.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * author: dz
 * 2022-10-20-15:23
 */

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderDetailsMapper orderDetailsMapper;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ResultVo insert(Goods goods) {
        ListOperations opsForList = redisTemplate.opsForList();
        if (opsForList.rightPop(goods.getName()) == null) {
            return new ResultVo<>(200, "ok", "noGoods");
        }
        opsForList.rightPop(goods.getName());

        Order order = new Order();
        String no = UUID.randomUUID().toString().replaceAll("-", "");
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(date.format(new Date()));
        order.setNo(no);
        order.setOrderTime(date.format(new Date()));
        order.setPeople("校园驿站代收");
        order.setAddress("学府楼菜鸟驿站");
        order.setPhone("17311687718");
        order.setStatus("y");
        orderMapper.insert(order);
        System.out.println(order);
        List<Order> order1 = orderMapper.getByCondition(order);
        Order order2 = order1.get(0);
        OrderDetails orderDetails = new OrderDetails();
        orderDetails.setOrderId(order2.getId());
        orderDetails.setGoodsId(goods.getId());
        orderDetails.setNums(1);
        orderDetails.setPrice(goods.getPrice());
        orderDetailsMapper.insert(orderDetails);
        System.out.println(orderDetails);
        return ResultVo.SUCCESS;
    }

    @Override
    public List<Order> getByCondition(Order order) {
        return orderMapper.getByCondition(order);
    }

    @Override
    public Order getById(Integer id) {
        return orderMapper.getById(id);
    }

    @Override
    public void updateById(Order order) {
        orderMapper.updateById(order);
    }

    @Override
    public void deleteById(Integer id) {
        orderMapper.deleteById(id);
    }

    @Override
    public List<Order> getByUserId(Integer userId) {
        return orderMapper.getByUserId(userId);
    }
}
